package flappy.Game;

import com.badlogic.gdx.*;
import com.badlogic.gdx.graphics.*;
import com.badlogic.gdx.graphics.g2d.*;
import com.badlogic.gdx.graphics.glutils.*;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer.*;

import flappy.TienIch.ScrollHandler;
import flappy.VatThe.*;

public class GameRenderer {
	
	private GameWorld          game;
	private OrthographicCamera camera;
	private ShapeRenderer      renderer;
	
	private SpriteBatch        spBatch;
	
	private int                y;
	private Chim               chim;
	private ScrollHandler      scroller;
	private MatDat             matDat1, matDat2;
	private OngNuoc            ong1, ong2, ong3;
	
	private TextureRegion      taptap, canhVat, matDat, conChim, dauOngNuoc, dauOngNuocNguoc, ongNuoc, ready, flappyBird, gameOver, bangDiem;
	private Animation          dsHinhChim;
	
	public GameRenderer(GameWorld game, int gameHeight, int midPointY) {
		this.game = game;
		
		this.y = midPointY;
		
		camera = new OrthographicCamera();
		camera.setToOrtho(true, 144, gameHeight);
		
		spBatch = new SpriteBatch();
		spBatch.setProjectionMatrix(camera.combined);
		renderer = new ShapeRenderer();
		renderer.setProjectionMatrix(camera.combined);
		
		KhoiTaoTaiNguyen();
		new Color();
	}
	
	
	public void KhoiTaoTaiNguyen(){
		TaiNguyen.KhoiTaoNgauNhien();
		
		chim = game.chim;
		scroller = game.scroller;
		matDat1 = scroller.matDat1;
		matDat2 = scroller.matDat2;
		ong1 = scroller.ongNuoc1;
		ong2 = scroller.ongNuoc2;
		ong3 = scroller.ongNuoc3;
		canhVat = TaiNguyen.canhVat;
		matDat = TaiNguyen.matDat;
		dsHinhChim = TaiNguyen.dsHinhChim;
		conChim = TaiNguyen.chim;
		dauOngNuoc = TaiNguyen.dauOngNuoc;
		dauOngNuocNguoc = TaiNguyen.dauOngNuocNguoc;
		ongNuoc = TaiNguyen.ongNuoc;
		ready = TaiNguyen.ready;
		flappyBird = TaiNguyen.flappyBird;
		gameOver = TaiNguyen.gameOver;
		bangDiem = TaiNguyen.bangDiem;
		taptap = TaiNguyen.taptap;
	}
	
	
	private void veMatDat() {
		spBatch.draw(matDat, matDat1.getX(), matDat1.getY(), matDat1.getWidth(), matDat1.getHeight());
		spBatch.draw(matDat, matDat2.getX(), matDat2.getY(), matDat2.getWidth(), matDat2.getHeight());
	}
	
	private void veOngNuoc() {	
		spBatch.draw(ongNuoc, ong1.getX(), ong1.getY(), ong1.getWidth(), ong1.getHeight());
		spBatch.draw(ongNuoc, ong1.getX(), ong1.getY() + ong1.getHeight() + TaiNguyen.KhoangCach2DauOng, ong1.getWidth(), y + 66 - (ong1.getHeight() + TaiNguyen.KhoangCach2DauOng));
		
		spBatch.draw(ongNuoc, ong2.getX(), ong2.getY(), ong2.getWidth(), ong2.getHeight());
		spBatch.draw(ongNuoc, ong2.getX(), ong2.getY() + ong2.getHeight() + TaiNguyen.KhoangCach2DauOng, ong2.getWidth(), y + 66 - (ong2.getHeight() + TaiNguyen.KhoangCach2DauOng));
		
		spBatch.draw(ongNuoc, ong3.getX(), ong3.getY(), ong3.getWidth(), ong3.getHeight());
		spBatch.draw(ongNuoc, ong3.getX(), ong3.getY() + ong3.getHeight() + TaiNguyen.KhoangCach2DauOng, ong3.getWidth(), y + 66 - (ong3.getHeight() + TaiNguyen.KhoangCach2DauOng));
		
		spBatch.draw(dauOngNuoc, ong1.getX() - 1, ong1.getY() + ong1.getHeight() - 14, 24, 14);
		spBatch.draw(dauOngNuocNguoc, ong1.getX() - 1, ong1.getY() + ong1.getHeight() + TaiNguyen.KhoangCach2DauOng, 24, 14);
		
		spBatch.draw(dauOngNuoc, ong2.getX() - 1, ong2.getY() + ong2.getHeight() - 14, 24, 14);
		spBatch.draw(dauOngNuocNguoc, ong2.getX() - 1, ong2.getY() + ong2.getHeight() + TaiNguyen.KhoangCach2DauOng, 24, 14);
		
		spBatch.draw(dauOngNuoc, ong3.getX() - 1, ong3.getY() + ong3.getHeight() - 14, 24, 14);
		spBatch.draw(dauOngNuocNguoc, ong3.getX() - 1, ong3.getY() + ong3.getHeight() + TaiNguyen.KhoangCach2DauOng, 24, 14);
		
	}
	
	private void veChimDungYen(float runTime) {
		spBatch.draw(dsHinhChim.getKeyFrame(runTime), 59, chim.y() - 15, chim.rong / 2.0f, chim.dai / 2.0f, chim.rong, chim.dai, 1, 1, chim.gocXoay);
	}
	
	private void veChim(float runTime) {
		
		if (chim.VoCanh()) {
			spBatch.draw(conChim, chim.x(), chim.y(), chim.rong / 2.0f, chim.dai / 2.0f, chim.rong, chim.dai, 1, 1, chim.gocXoay);
			
		} else {
			spBatch.draw(dsHinhChim.getKeyFrame(runTime), chim.x(), chim.y(), chim.rong / 2.0f, chim.dai / 2.0f, chim.rong, chim.dai, 1, 1, chim.gocXoay);
		}
		
	}
	
	private void veGiaoDienChaoMung() {
		spBatch.draw(flappyBird, 37, y - 50, flappyBird.getRegionWidth() / 1.2f, flappyBird.getRegionHeight() / 1.2f);
	
	}
	
	private void veBangDiem() {
		spBatch.draw(bangDiem, 22, y - 30, 97, 37);
		
		TaiNguyen.fontDiemXepHang.draw(spBatch, "" + game.diem, 33, y - 17);
		
		TaiNguyen.fontDiemXepHang.draw(spBatch, "" + TaiNguyen.LayDiemCaoNhat(), 97, y - 17);
	}
	
	private void veTrangThaiSanSang() {
		spBatch.draw(ready, 36, y - 50, 68, 14);
		
		spBatch.draw(taptap, 40, y - 20, taptap.getRegionWidth(), taptap.getRegionHeight());
		
	}
	
	private void veGameOver() {
		spBatch.draw(gameOver, 24, y - 50, 92, 14);
	}
	
	private void veDiem() {
		int length = ("" + game.diem).length();
		TaiNguyen.fontDiemDaChoi.draw(spBatch, "" + game.diem, 68 - (3 * length), y - 83);
	}
	
	public void render(float delta, float t) {
		
		Gdx.gl.glClearColor(0, 0, 0, 1);
		Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
		
		renderer.begin(ShapeType.Filled);
		
		
		//vẽ bù mặt đất nếu độ phân giải màn hình quá dài
		renderer.setColor(222 / 255.0f, 215 / 255.0f, 152 / 255.0f, 1);
		renderer.rect(0, 0, 144, 300);

		renderer.end();
				
		spBatch.begin();
		spBatch.draw(canhVat, 0, 0, 144, 200);
		
		veOngNuoc();
		
		switch (game.TrangThai) {
			case DangChoi:
				veChim(t);
				veDiem();
				break;
			
			case SanSang:
				veChim(t);
				veTrangThaiSanSang();
				break;
			
			case ChaoMung:
				veChimDungYen(t);
				veGiaoDienChaoMung();
				break;
			
			case GameOver:
				veBangDiem();
				veChim(t);
				veGameOver();
				break;
			case XemDiem:
				veBangDiem();
				veChim(t);
				break;
			default:
				break;
		}
		
		veMatDat();
		
		spBatch.end();
	}
	
}
